//
//  AdDetailViewController.m
//  PlayBall
//
//  Created by admin on 14-7-25.
//
//

#import "AdDetailViewController.h"
#import "JddGlobal.h"
#import "JddViewControllerManager.h"
#import "JddCommonFunction.h"
#import "JddRechargeWaySelectViewController.h"
#import "AppDelegate.h"
#import "UIFitButton.h"

@interface AdDetailViewController ()
{
    UIWebView *_webView;
    JddRechargeWaySelectViewController *reCharge;
}

@end

@implementation AdDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setJddTabBarHidden:YES];//隐藏tabbar
//    [self.navigationItem setHidesBackButton:YES];//隐藏返回按钮
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    customLab.backgroundColor = [UIColor clearColor];
    [customLab setText:self.alt];
    customLab.textAlignment = NSTextAlignmentCenter;
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.font = [UIFont systemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    
    //创建返回button
//    [self setBackBtn];
    //初始化webView
    if ([JddCommonFunction checkNetWork] == true) {
        [self initWebView];
    }else{
        [JddCommonFunction setWaitingViewHidden:NO fromController:self];
    }
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)setBackBtn
{
    UIFitButton *backButton = [UIFitButton buttonWithType:UIButtonTypeCustom];
    backButton.isleftBtn = YES;
    backButton.frame = CGRectMake(0.0, 0.0, 12, 24);
    [backButton setImage:[UIImage imageNamed:@"navbar_back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    temporaryBarButtonItem.style = UIBarButtonItemStylePlain;
    self.navigationItem.leftBarButtonItem = temporaryBarButtonItem;

}
- (void)backBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
}

- (void)initWebView
{
    if([self checkDevice:@"iPhone"]){
        if(IOS7){
            _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64)];
        }else{
            _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-44)];
        }
    }else{
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    }
    _webView.delegate = self;
    _webView.scalesPageToFit = YES;
    NSArray *array = [_webView subviews];
    UIScrollView *scrollView =(UIScrollView *) [array objectAtIndex:0];
    scrollView.bounces = NO;
    NSURL *url = [NSURL URLWithString:self.linkUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    
    //这边接口直接写死，不要从广告下载那里传值过来
//    if (_num == 0) {
//       NSURL *url = [NSURL URLWithString:@"http://10.33.97.199:82/app/go/140610/index.html"];
//       NSURLRequest *request = [NSURLRequest requestWithURL:url];
//       [_webView loadRequest:request];
//    }else if(_num == 1){
//        NSURL *url = [NSURL URLWithString:@"http://10.33.97.199:82/app/go/140801/jc_main.html"];
//         NSURLRequest *request = [NSURLRequest requestWithURL:url];
//        [_webView loadRequest:request];
//    }else if (_num == 2){
//        NSURL *url = [NSURL URLWithString:@"http://10.33.97.199:82/app/go/140801/jc_index.html"];
//         NSURLRequest *request = [NSURLRequest requestWithURL:url];
//        [_webView loadRequest:request];
//    }
    
    [self.view addSubview:_webView];
}

-(bool)checkDevice:(NSString*)name

{
    NSString* deviceType = [UIDevice currentDevice].model;
    NSRange range = [deviceType rangeOfString:name];
    return range.location != NSNotFound;
}

//有网的时候才会执行webView的协议方法
- (void)webViewDidStartLoad:(UIWebView *)webView
{
   [JddCommonFunction setWaitingViewHidden:NO fromController:self];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    
}

#pragma mark 这里需要继续写的地方
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    AppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];
    if ([request.mainDocumentURL.relativePath isEqualToString:@"/app/demo.html1|100_60|1"]) {
        appDelegate.moneyNum = @"100";
        appDelegate.redNum = @"60";
        [self rechargeButtonClicked];
        return false;
    }else if ([request.mainDocumentURL.relativePath isEqualToString:@"/app/demo.html1|1000_150|2"]){
        appDelegate.moneyNum = @"1000";
        appDelegate.redNum = @"150";
        [self rechargeButtonClicked];
        return false;
    }else if ([request.mainDocumentURL.relativePath isEqualToString:@"/app/demo.html6|98_4"]){
        appDelegate.moneyNum = @"98";
        appDelegate.redNum = @"4";
        [self rechargeButtonClicked];
        return false;
    }else if ([request.mainDocumentURL.relativePath isEqualToString:@"/app/demo.html6|998_50"]){
        appDelegate.moneyNum = @"998";
        appDelegate.redNum = @"50";
        [self rechargeButtonClicked];
        return false;
    }else if ([request.mainDocumentURL.relativePath isEqualToString:@"/app/demo.html6|9998_510"]){
        appDelegate.moneyNum = @"9998";
        appDelegate.redNum = @"510";
        [self rechargeButtonClicked];
        return false;
    }else if ([request.mainDocumentURL.relativePath isEqualToString:@"/app/demo.html6|29998_1590"]){
        appDelegate.moneyNum = @"29998";
        appDelegate.redNum = @"1590";
        [self rechargeButtonClicked];
        return false;
    }else if ([request.mainDocumentURL.relativePath isEqualToString:@"/app/demo.html6|59998_3360"]){
        appDelegate.moneyNum = @"59998";
        appDelegate.redNum = @"3360";
        [self rechargeButtonClicked];
        return false;
    }else if ([request.mainDocumentURL.relativePath isEqualToString:@"/app/demo.html6|99998_5990"]){
        appDelegate.moneyNum = @"99998";
        appDelegate.redNum = @"5990";
        [self rechargeButtonClicked];
        return false;
    }else if ([request.mainDocumentURL.relativePath isEqualToString:@"/app/demo.html2|90"]){
        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_PICK_NUM_SPORT
                                                                fromController:self
                                                                    withObject:nil
                                                                   withObject2:nil];
        return false;
    }
    return true;
}

- (void)rechargeButtonClicked{
    if (loginState==false) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"您还没有登录，请先登录"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:@"取消",nil ];
        [alert show];
        
    } else {
        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_SELECT_RECHARGE_WAY
                                                                fromController:self
                                                                    withObject:nil
                                                                   withObject2:nil];
        
    }
}
// alertViewDelegate 警告框button被点击
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ([alertView.message isEqualToString:@"您还没有登录，请先登录"]) {
        if (buttonIndex == 0) {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_LOGIN
                                                                    fromController:self
                                                                        withObject:nil
                                                                       withObject2:nil];
        }else{
            alertView.hidden = YES;
        }
        
    }
    
}

- (void)viewWillAppear:(BOOL)animated
{
    if ([JddCommonFunction isLogin]) {
        loginState = true;
    }else {
        loginState = false;
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
